package com.uenpay.im.ui.old.activity.publicnumber;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-8 下午2:04:40
 * @copyRight http://www.qiyunxin.com
 * @function 搜索公众号
 */
public class SearchPublicNumActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {
    private PublicNumberAdapter numberAdapter = null;
    private EditText searchEt;
    private RefreshListView listView;
    private TextView no_data_tv;
    private View loading;
    private int page = 1;
    private String keyWord = "";
    private PublicNumHandle mPublicNumHandle = new PublicNumHandle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_search_publicnum_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        Utils.showSoftKeyboard(searchEt);
    }

    protected void initView() {
        searchEt = (EditText) findViewById(R.id.searchEt);
        listView = (RefreshListView) findViewById(R.id.discover_listview);
        no_data_tv = (TextView) findViewById(R.id.no_data_tv);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        listView.setPullLoadEnable(true);
        listView.setRefreshListViewListener(this);
        searchEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyWord = searchEt.getText().toString();
                    page = 1;
                    searchPublicNum();
                    Utils.hideSoftKeyboard(searchEt);
                    return true;
                }
                return false;
            }
        });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                PublicNumEntity numEntity = (PublicNumEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (numEntity != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("cust_id", numEntity.custid);
                    ActivityHandle.startActivity(SearchPublicNumActivity.this,
                            PublicNumberDetailActivity.class, bundle, -1);
                }
            }
        });
    }

    /**
     * 搜索公众号
     */
    private void searchPublicNum() {
        mPublicNumHandle.searchPublicNums(page, keyWord,
                new PublicNumHandle.ISearchPublicNumListener() {

                    @Override
                    public void onSearchPublicNumResult(int status, String msg,
                                                        ArrayList<PublicNumEntity> arrayList) {
                        myHandler.sendEmptyMessage(0);
                        onLoad();
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (page == 1) {
                                if (numberAdapter == null) {
                                    numberAdapter = new PublicNumberAdapter(
                                            SearchPublicNumActivity.this,
                                            arrayList);
                                    listView.setAdapter(numberAdapter);
                                } else {
                                    numberAdapter.setData(arrayList);
                                }
                                listView.setPullLoadEnable(true);
                            } else {
                                if (arrayList != null && arrayList.size() > 0) {
                                    numberAdapter.addData(arrayList);
                                } else {
                                    listView.setPullLoadEnable(false);
                                }
                            }
                            no_data_tv.setVisibility(View.GONE);
                            listView.setVisibility(View.VISIBLE);
                            if (arrayList == null || arrayList.size() < 1
                                    && page == 1) {
                                no_data_tv.setVisibility(View.VISIBLE);
                                listView.setVisibility(View.GONE);
                            }

                        } else {
                            if (page == 1) {
                                no_data_tv.setVisibility(View.VISIBLE);
                                listView.setVisibility(View.GONE);
                            } else {
                                listView.setPullLoadEnable(false);
                            }
                        }
                    }
                });
    }

    public Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }

            return false;
        }
    });

    private void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        searchPublicNum();
    }

    @Override
    public void onLoadMore() {
        page++;
        searchPublicNum();
    }
}
